#include "StdAfx.h"
#include "RegisterAppInfo.h"
#include <stdio.h>

RegisterAppInfo::RegisterAppInfo(void)
{
	ZeroMemory(m_deviceSerialNum, TAX_DEVICE_SERIAL_LENGTH);
	ZeroMemory(m_appInstallPath, MAX_PATH);
	ZeroMemory(m_recoverLinkPathName, MAX_PATH);
	ZeroMemory(m_appKeyName, FAX_APP_NAME_LENGTH);
	ZeroMemory(m_appExePath, MAX_PATH);
}

RegisterAppInfo::~RegisterAppInfo(void)
{
}

BOOL RegisterAppInfo::InitByString(char* line)
{
	int len = strlen(line), start = 0, index = 0, maxLen = 0;
	char *pDist = NULL;
	for (int i = 0; i < len; i++)
	{
		if ('\t' == line[i])
		{
			line[i] = 0;
			switch (index)
			{
			case 0:
				pDist = m_appKeyName;
				maxLen = FAX_APP_NAME_LENGTH;
				break;
			case 1:
				pDist = m_appInstallPath;
				maxLen = MAX_PATH;
				break;
			case 2:
				pDist = m_appExePath;
				maxLen = MAX_PATH;
				break;
			case 3:
				pDist = m_deviceSerialNum;
				maxLen = TAX_DEVICE_SERIAL_LENGTH;
				break;			
			default:
				return FALSE;
			}
			strcpy_s(pDist, maxLen, line + start);
			start = i + 1;
			index++;
		}
	}
	
	pDist = m_recoverLinkPathName;
	maxLen = MAX_PATH;
	strcpy_s(pDist, maxLen, line + start);	
	if (0 == strlen(m_appExePath) || 0 == strlen(m_appInstallPath) || 0 == strlen(m_appKeyName) || 0 == strlen(m_deviceSerialNum))
	{
		return FALSE;
	}
	return TRUE;
}

void RegisterAppInfo::SetAppInstallPath( char *appInstallPath )
{
	if (!appInstallPath || strlen(appInstallPath) >= MAX_PATH)
	{
		return;
	}
	strcpy_s(m_appInstallPath, MAX_PATH, appInstallPath);
	//::CharLowerA(m_appInstallPath);
}

void RegisterAppInfo::SetRecoverLinkPathName( char *recoverLinkPathName )
{
	if (!recoverLinkPathName || strlen(recoverLinkPathName) >= MAX_PATH)
	{
		return;
	}
	strcpy_s(m_recoverLinkPathName, MAX_PATH, recoverLinkPathName);
}

void RegisterAppInfo::SetDeviceSerialNum( char *deviceSerialNum )
{
	if (!deviceSerialNum || strlen(deviceSerialNum) >= TAX_DEVICE_SERIAL_LENGTH)
	{
		return;
	}
	strcpy_s(m_deviceSerialNum, TAX_DEVICE_SERIAL_LENGTH, deviceSerialNum);
}

void RegisterAppInfo::SetAppKeyName( char *appKeyName )
{
	if (!appKeyName || strlen(appKeyName) >= FAX_APP_NAME_LENGTH)
	{
		return;
	}
	strcpy_s(m_appKeyName, FAX_APP_NAME_LENGTH, appKeyName);
}

BOOL RegisterAppInfo::SameSerialNum( char *serialNum )
{
	if (strlen(m_deviceSerialNum) == strlen(serialNum) &&
		0 == _stricmp(m_deviceSerialNum, serialNum))
	{
		return TRUE;
	}
	return FALSE;
}

void RegisterAppInfo::SetAppExePath( char *appExePath )
{
	if (!appExePath || strlen(appExePath) >= MAX_PATH)
	{
		return;
	}
	strcpy_s(m_appExePath, MAX_PATH, appExePath);
	//::CharLowerA(m_appExePath);
}
//BOOL RegisterAppInfo::CopyToLine( char *line, int bufferLen )
//{
//	if(!line || 0 == strlen(m_appInstallPath) || 0 == strlen(m_appKeyName) || 0 == strlen(m_deviceSerialNum))
//	{
//		return FALSE;
//	}
//	if (bufferLen < strlen(m_appInstallPath) + strlen(m_appKeyName) + strlen(m_deviceSerialNum) + strlen(m_recoverLinkPathName) + 4)
//	{
//		return FALSE;
//	}
//	sprintf_s(line, bufferLen, "%s\t%s\t%s\t%s", m_appKeyName, m_appInstallPath, m_deviceSerialNum, m_recoverLinkPathName);
//	return TRUE;
//}

RegisterAppInfo *CreateRegisterAppInfoObject()
{
	return new RegisterAppInfo();
}
